/*
 * Created by JFormDesigner on Tue Jun 20 09:26:10 CST 2023
 */

package org.scct.chenhui.frames;

import org.scct.chenhui.dao.UserDao;
import org.scct.chenhui.model.UserBean;

import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;

/**
 * @author Administrator
 */
public class FrmLogin extends JFrame {
    public static void main(String[] args) {
        FrmLogin frmLogin = new FrmLogin();
        frmLogin.setVisible(true);
//        设置窗口关闭的行为
        frmLogin.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public FrmLogin() {
        initComponents();
        initData();
    }

    private void initData() {
      btnLogin.addActionListener(new AbstractAction() {
          @Override
          public void actionPerformed(ActionEvent e) {
//              System.out.println("点击了按钮");
             // 获取窗口中用户输入的内容
              String name = txtName.getText();
              String pass = txtPass.getText();
              int role=radUser.isSelected()?1:2;
//              用户名  3-10个 数字，字母或下划线
//              密码     6-10  数字，字母或下划线
//              trim()删除前后空格
             if(!name.trim().matches("^\\w{3,10}$")) {
                  JOptionPane.showMessageDialog(null,"请输入有效的用户名");
                  return;
             }
              if(!pass.trim().matches("^\\w{6,10}$"))  {
                  JOptionPane.showMessageDialog(null,"请输入有效的密码");
                  return;
              }
              UserDao userDao = new UserDao();
              UserBean user = userDao.Login(name, pass, role);
              if(user==null)
              {
                  JOptionPane.showMessageDialog(null,"用户名，密码或用户类型错误，请检查后重新登录");
              }else{
                  if(user.getRole()==1)
                  {
                      System.out.println("打开普通用户的借阅管理窗口");
                  }else{
                      FrmAdminMain frmAdminMain = new FrmAdminMain();
                      frmAdminMain.setVisible(true);
//                      关掉当前的窗口
                      dispose();
                  }
              }

          }
      });
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - brightch
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        txtName = new JTextField();
        txtPass = new JPasswordField();
        label4 = new JLabel();
        radUser = new JRadioButton();
        radAdmin = new JRadioButton();
        btnLogin = new JButton();
        btnRegister = new JButton();

        //======== this ========
        setResizable(false);
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u7528\u6237\u767b\u5f55");
        label1.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 18));
        label1.setForeground(Color.blue);
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(225, 45), label1.getPreferredSize()));

        //---- label2 ----
        label2.setText("\u7528\u6237\u540d");
        label2.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));
        contentPane.add(label2);
        label2.setBounds(90, 110, label2.getPreferredSize().width, 20);

        //---- label3 ----
        label3.setText("\u5bc6\u7801");
        label3.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));
        contentPane.add(label3);
        label3.setBounds(new Rectangle(new Point(90, 155), label3.getPreferredSize()));
        contentPane.add(txtName);
        txtName.setBounds(170, 105, 225, 40);
        contentPane.add(txtPass);
        txtPass.setBounds(170, 150, 225, 40);

        //---- label4 ----
        label4.setText("\u7528\u6237\u7c7b\u578b");
        label4.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));
        contentPane.add(label4);
        label4.setBounds(90, 200, label4.getPreferredSize().width, 25);

        //---- radUser ----
        radUser.setText("\u666e\u901a\u7528\u6237");
        radUser.setSelected(true);
        contentPane.add(radUser);
        radUser.setBounds(new Rectangle(new Point(180, 205), radUser.getPreferredSize()));

        //---- radAdmin ----
        radAdmin.setText("\u7ba1\u7406\u5458");
        contentPane.add(radAdmin);
        radAdmin.setBounds(new Rectangle(new Point(300, 205), radAdmin.getPreferredSize()));

        //---- btnLogin ----
        btnLogin.setText("\u767b\u5f55");
        contentPane.add(btnLogin);
        btnLogin.setBounds(new Rectangle(new Point(145, 280), btnLogin.getPreferredSize()));

        //---- btnRegister ----
        btnRegister.setText("\u6ce8\u518c");
        contentPane.add(btnRegister);
        btnRegister.setBounds(new Rectangle(new Point(270, 280), btnRegister.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(530, 390);
        setLocationRelativeTo(getOwner());

        //---- buttonGroup1 ----
        var buttonGroup1 = new ButtonGroup();
        buttonGroup1.add(radUser);
        buttonGroup1.add(radAdmin);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }
    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - brightch
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JTextField txtName;
    private JPasswordField txtPass;
    private JLabel label4;
    private JRadioButton radUser;
    private JRadioButton radAdmin;
    private JButton btnLogin;
    private JButton btnRegister;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
